package karileet.meeting.profile;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Class contains methods for retrieving/saving app states from/to shared 
 * preferences
 * 
 * @author markus karileet
 */
public class ProfileConf {
	public static final String PREFS_NAME = "MeetingProfilePrefsFile";
	public static String APP = "Meeting profile";
	public static String PROFILE = "profile";
	public static String VIBRATE = "vibrate";
	public static String START = "start";
	public static String STOP = "stop";
	public static String STARTENABLED = "startenabled";
	public static String STOPENABLED = "stopenabled";
	public static String BEEP = "beep";
	private Context ctx;
	
	/**
	 * Constructor inits object and populates values from shared preferences
	 * (when existing)
	 * @param ctx
	 */
	public ProfileConf(Context ctx) {
		this.ctx = ctx;
	}
	
	/**
	 * @return int Volume of beep
	 */
	public int getBeepVolume() {
		return getIntPreference(BEEP);
	}

	/**
	 * Method stores user defined beep volume
	 * @param int beepVolume Volume of beep
	 */
	public void setBeepVolume(int beepVolume) {
		editPreferences(BEEP, beepVolume);
	}

	/**
	 * @return boolean whether app is enabled or not
	 */
	public boolean isProfileEnabled() {
		return getBoolPreference(PROFILE);
	}
	
	/**
	 * Method stores profile state to shared preferences
	 * @param boolean profileEnabled Whether app is enabled or not
	 */
	public void setProfileEnabled(boolean profileEnabled) {
    	editPreferences(PROFILE, profileEnabled);
	}
	
	/**
	 * @return boolean whether vibrate is enabled or not
	 */
	public boolean isVibrateEnabled() {
		return getBoolPreference(VIBRATE);
	}
	
	/**
	 * Method stores vibrate state to shared preferences
	 * @param boolean vibrateEnabled Whether vibrate is enabled or not
	 */
	public void setVibrateEnabled(boolean vibrateEnabled) {
		editPreferences(VIBRATE, vibrateEnabled);
	}	
	
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, boolean state) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putBoolean(type, state).commit();
	}
	/**
	 * Method commits state under type to sharedPreferences
	 * @param String type Preference name
	 * @param boolean state State to save
	 */
	private void editPreferences(String type, int value) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putInt(type, value).commit();
	}
	
	private int getIntPreference(String type) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getInt(type, 0);
	}
	
	private boolean getBoolPreference(String type) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getBoolean(type, false);
	}
	
	public void setScheduleStopEnabled(boolean stopEnabled) {
		editPreferences(STOPENABLED, stopEnabled);
	}
	public void setScheduleStart(long start) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putLong(START, start).commit();
	}
	public void setScheduleStop(long stop) {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		p.edit().putLong(STOP, stop).commit();
	}
	public long getScheduleStart() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getLong(START, 0);
	}
	public long getScheduleStop() {
		SharedPreferences p = ctx.getSharedPreferences(PREFS_NAME, 0);
		return p.getLong(STOP, 0);
	}
	
	
	public void setScheduleStartEnabled(boolean startEnabled) {
		editPreferences(STARTENABLED, startEnabled);
	}
	
	public boolean isScheduleStartEnabled() {
		return getBoolPreference(STARTENABLED);
	}
	
	public boolean isScheduleStopEnabled() {
		return getBoolPreference(STOPENABLED);
	}
}